from industrialai.api.expose import expose as wsexpose
from industrialai.pkg.page import get_page
from pecan import expose
from pecan import request
from pecan.rest import RestController
from pecan_swagger import decorators as swagger
from wsme import types as wstypes


class Roletest(wstypes.Base):
    message = wstypes.text
    roleId = int


@swagger.path("roles", "Role", "Root")
class RoleController(RestController):

    _custom_actions = {
        'test': ['POST']
    }

    """
       @api {get} /v1/roles get_all_roles
       @apiName GetAllRoles
       @apiGroup Role

       @apiSuccess {Object[]} roles
       @apiSuccessExample {type} Success-Response:
            HTTP/1.1 200 OK
            [
                “data":[                        角色列表
                    {
                        "title": "expert",      角色名称
                        "id": 1                 角色id
                    },
                ]
                "pages": 12                     总页数
                “total": 10                     总数
            ]
    """
    @expose('json')
    def get_all(self):
        page_num, page_size = get_page(request.body)
        db_conn = request.db_conn
        roles, num = db_conn.list_roles(page_num, page_size)
        return {
            "data": roles,
            "pages": num
        }

    @wsexpose(Roletest, body=Roletest)
    def test(self, role):
        print(role)
        role.message = "out"
        role.roleId = 1
        print(role)
        return role
